package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.InvestmentListAdapter.ViewHolder;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Investment;
import com.hmyd.hmyd_android.easydialog.EasyDialog;
import com.hmyd.hmyd_android.fragment.InvestmentTypeFragment;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.DynamictextTextview;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class InvestmentListTypeAdapter extends BaseAdapter {

	private Context mContext;
	private List<Investment> mPlist;
	private LayoutInflater mInflater;
	private String mType;
	private EasyDialog dialog;

	private InvestmentTypeFragment mBasicFragment;

	public InvestmentListTypeAdapter(Context context, List<Investment> plist, String type, InvestmentTypeFragment basicFragment){
		super();

		this.mContext = context;
		this.mPlist = plist;
		mInflater=LayoutInflater.from(context);
		mType = type;
		mBasicFragment = basicFragment;
		dialog = new EasyDialog(context);
	}

	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public Investment getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(mPlist==null || mPlist.get(position)==null){
			return convertView;
		}

		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_investment_list_type_item, null);
			holder = new ViewHolder();

			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.type = (ImageView) convertView.findViewById(R.id.type);
			holder.investAmount = (TextView) convertView.findViewById(R.id.investAmount);

			holder.investment_rate = (DynamictextTextview) convertView.findViewById(R.id.interest_rate);
			holder.investment_rate_sp = (DynamictextTextview) convertView.findViewById(R.id.investment_rate_sp);
			holder.investment_rate_tag = (DynamictextTextview) convertView.findViewById(R.id.investment_rate_tag);

			holder.income = (TextView) convertView.findViewById(R.id.income);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.period = (TextView) convertView.findViewById(R.id.period);
			holder.cancelOrder = (Button) convertView.findViewById(R.id.cancel_order);
			holder.ib_hui = (ImageButton) convertView.findViewById(R.id.lie_bi_hui);
			holder.ib_yue = (ImageButton) convertView.findViewById(R.id.lie_bi_yue);
			holder.ib_quan = (ImageButton) convertView.findViewById(R.id.lie_bi_quan);
			holder.ib_piao = (ImageButton) convertView.findViewById(R.id.lie_bi_piao);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		final Investment investment = getItem(position);
		if(investment!=null){
			holder.title.setText(investment.title);

			if(investment.type.equals("shf")){
				holder.type.setImageResource(R.drawable.tou_lei_shou);
				holder.ib_piao.setVisibility(View.GONE);
			}else if(investment.type.equals("phb")){
				holder.type.setImageResource(R.drawable.tou_lei_pu);
				holder.ib_piao.setVisibility(View.GONE);
			}else if(investment.type.equals("hht")){
				holder.type.setImageResource(R.drawable.tou_lei_hong);
				holder.ib_piao.setVisibility(View.GONE);
			} else if(investment.type.equals("grpj")) {
				holder.type.setImageResource(R.drawable.piao_icon_piao);
				holder.ib_piao.setVisibility(View.VISIBLE);
				holder.ib_piao.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						showKFServer("个人票据标", v);
					}
				});
			}
			
			// 年化率
			getRateView(investment, holder);

			holder.investAmount.setText(Utils.amountConversionFormat(Double.valueOf(investment.investAmount)));

			holder.income.setText(Utils.amountConversionFormat(Double.valueOf(investment.income)));

			if("paying".equals(mType)){//paying(正在回款），investing（正在投标）,finished（已结清
				if("grpj".equals(investment.type)) {
					holder.time.setText(Utils.getDateStringDay(investment.startTime)+"起息  ");
				} else {
					holder.time.setText(Utils.getDateStringDay(investment.startTime)+"起息  "+Utils.getDateStringDay(investment.endTime)+"到期");
				}
				holder.cancelOrder.setVisibility(View.GONE);
			}else if("investing".equals(mType)){

				holder.time.setText(Utils.getDateStringMinute(investment.investTime)+"起投");
				holder.cancelOrder.setVisibility(View.GONE);
			}else if("finished".equals(mType)){
				if("grpj".equals(investment.type)) {
					holder.time.setText(Utils.getDateStringDay(investment.startTime)+"起息  ");
				} else {
					holder.time.setText(Utils.getDateStringDay(investment.startTime)+"起息  "+Utils.getDateStringDay(investment.endTime)+"到期");
				}
				holder.cancelOrder.setVisibility(View.GONE);
			}else if("order".equals(mType)){
				holder.cancelOrder.setVisibility(View.VISIBLE);
				holder.time.setText(Utils.getDateStringMinute(investment.investTime)+"预约");
				if(investment.isCanceled){
					holder.cancelOrder.setText("已取消");
					holder.cancelOrder.setBackgroundDrawable(null);
					holder.cancelOrder.setOnClickListener(null);

					holder.investment_rate.setTextColor(mContext.getResources().getColor(R.color.register_protocol));
					holder.investAmount.setTextColor(mContext.getResources().getColor(R.color.register_protocol));
					holder.income.setTextColor(mContext.getResources().getColor(R.color.register_protocol));
					holder.time.setTextColor(mContext.getResources().getColor(R.color.register_protocol));
					holder.period.setTextColor(mContext.getResources().getColor(R.color.register_protocol));

				}else{
					holder.cancelOrder.setText("取消预约");
					holder.cancelOrder.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							mBasicFragment.onRequest(InvestmentTypeFragment.DODEPOSIT, UrlConstant.SENDCANCELORDERVERIFYCODE, HttpMethod.GET, null, position);
						}
					});
				}
			}

			holder.period.setText(investment.period+Utils.periodConversion(investment.periodUnit));

			holder.ib_hui.setVisibility(View.GONE);
			holder.ib_quan.setVisibility(View.GONE);

			//如果有使用优惠券
			if (!TextUtils.isEmpty(investment.couponAmount)&& Double.valueOf(investment.couponAmount)>0) {
				holder.ib_quan.setVisibility(View.VISIBLE);

				holder.ib_quan.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						showKFServer("其中优惠券："+investment.couponAmount+"元", v);
					}
				});
			}else {
				holder.ib_quan.setVisibility(View.GONE);
			}
			//如果有使用小惠钱包
			if (!TextUtils.isEmpty(investment.useWalletAmount)&& Double.valueOf(investment.useWalletAmount)>0) {
				holder.ib_hui.setVisibility(View.VISIBLE);
				holder.ib_hui.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						showKFServer("其中小惠钱包："+investment.useWalletAmount+"元", v);
					}
				});
			}else {
				holder.ib_hui.setVisibility(View.GONE);
			}
		}
		return convertView;
	}
	
	private void getRateView(Investment investment, ViewHolder holder){
		String rate_value = investment.rate;
		String rate = "";
		String rate_sp = "";
		String rate_tag = "";

		// 如果包含%， 删除所有%  手动添加
		rate_value = rate_value.contains("%") ? rate_value.replace("%", "") : rate_value;

		if (rate_value.contains("+")) {
			rate = rate_value.split("[+]")[0];
			rate_tag = rate_value.split("[+]")[1];

			if (rate.contains(".")) {
				rate_sp = rate.split("[.]")[1];
				rate = rate.split("[.]")[0]+".";
			}

			holder.investment_rate_tag.setVisibility(View.VISIBLE);

			holder.investment_rate.setText(rate);
			holder.investment_rate_sp.setText(rate_sp+"%");
			holder.investment_rate_tag.setText("+"+rate_tag+"%");
		} else {
			if (rate_value.contains(".")) {
				rate_sp = rate_value.split("[.]")[1];
				rate = rate_value.split("[.]")[0]+".";
			} else {
				rate = rate_value;
			}

			holder.investment_rate_tag.setVisibility(View.GONE);

			holder.investment_rate.setText(rate);
			holder.investment_rate_sp.setText(rate_sp+"%");
		}
	}

	static class ViewHolder{
		private TextView title;
		private ImageView type;
		private TextView investAmount;

		private DynamictextTextview investment_rate;
		private DynamictextTextview investment_rate_sp; // 如果额外加的收益率显示label
		private DynamictextTextview investment_rate_tag; // 小数点后

		private TextView income;
		private TextView time;
		private TextView period;
		private Button cancelOrder;
		private ImageButton ib_hui;
		private ImageButton ib_yue;
		private ImageButton ib_quan;
		private ImageButton ib_piao;
	}

	/**
	 * 显示客服服务信息
	 */
	private void showKFServer(String content, View v){
		TextView tv = new TextView(mContext);
		tv.setText(content);
		tv.setTextColor(Color.WHITE);
		tv.setBackgroundColor(mContext.getResources().getColor(R.color.tran));
		tv.setPadding(20, 10, 20, 10);

		dialog = new EasyDialog(mContext)
		.setLayout(tv)														// 设置布局文件
		.setBackgroundColor(mContext.getResources().getColor(R.color.tran))// 设置布局背景
		.setLocationByAttachedView(v)										// 设置显示位置
		.setTouchOutsideDismiss(true)										// 设置触摸外部是否取消
		.setMatchParent(false)												// 设置是否填充
		.setMarginLeftAndRight(2, 0)										// 距离屏幕位置
		.setOutsideColor(Color.TRANSPARENT)									// 设置对话框外部背景颜色
		.show();
	}
}
